(*
This file is part of the utils section of Lustre reader

* Copyright (c) 2010, Nicola Camuffo.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of Nicola Camuffo, nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*)


(* handle, list, print function, separator *)
let rec list_iter h l f s = match l with
  [] -> ()
  | [x] -> f h x
  | x::xs -> f h x; output_string h s; list_iter h xs f s

(* handle, table, print function, separator *)
let hash_iter h t f s =
  let lst = Hashtbl.fold (fun k v l -> l @ [k,v]) t [] in
  list_iter h lst f s



let print_simple_constructor handle spaces name funct =
  output_string handle (spaces ^ name ^ "(");
  funct handle;
  output_string handle ")"

let print_complex_constructor handle spaces name funct =
  output_string handle (spaces ^ name ^ "(\n");
  funct handle;
  output_string handle ("\n" ^ spaces ^ ")")


let print_simple_list handle spaces lst funct =
  output_string handle spaces;
  list_iter handle lst funct ","

let print_complex_list handle spaces lst funct =
  output_string handle spaces;
  list_iter handle lst funct ("\n" ^ spaces)


let print_hash handle spaces tbl name funct =
  output_string handle (spaces ^ name ^ "\n");
  hash_iter handle tbl funct ("\n" ^ spaces)





